import mongoose from 'mongoose';
import { ApiProperty } from '@nestjs/swagger';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { User } from './user.schema';

@Schema({
  toJSON: {
    virtuals: true,
  },
  toObject: {
    virtuals: true,
  },
  timestamps: true,
})
export class Order extends Document {
  @ApiProperty({ description: '订单状态', example: 0 })
  @Prop({ required: true, default: 0 })
  status: number;

  @ApiProperty({ description: '用户ID', example: 'xxxxx' })
  @Prop({ required: true, ref: 'User', type: mongoose.Schema.Types.ObjectId })
  user: User;

  @ApiProperty({ description: '快递单号', example: 'xxxxx' })
  @Prop()
  express: string;
}

export type OrderDocument = Order & Document;

const OrderSchema = SchemaFactory.createForClass(Order);
OrderSchema.virtual('cars', {
  ref: 'Car',
  localField: '_id',
  foreignField: 'order',
  justOne: false,
});

export { OrderSchema };
